package com.slf.engine.threads;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.base.IBaseDao;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.IBossService;
import com.slf.engine.services.IHpBossService;
import com.slf.engine.services.impl.BossServiceImpl;
import com.slf.engine.services.impl.BusServiceImpl;
import com.slf.engine.services.impl.HpbossServiceImpl;
import com.slf.engine.utils.Factory;


public class GetRequestThread extends Thread {

	private static final org.apache.log4j.Logger log = Logger.getLogger(GetRequestThread.class);
	
	
	public GetRequestThread()
	{
		log.log(Priority.INFO, "GET_REQUEST_THREAD RUN SUCCESS");
	}
	
	public void run()
	{
		IBossService bossService = new BossServiceImpl();
//		IHpBossService bossService = new HpbossServiceImpl();
		while(true)
		{
//			List<Order> list = bossService.getListOrders();
			Order order = bossService.getOneOrder();
//			if(list != null)
			if(order != null)
			{
				IBaseDao baseDao = (IBaseDao) Factory.getInstantBiz("baseDao");
//				Order order = null;
//				for(int i = 0; i < list.size(); i++)
//				{
					try {
//						order = list.get(i);
						baseDao.add(order, "addOrder");
						log.log(Priority.INFO, "交易队列添加订单成功order_id:" + order.getOrderId() + " account:" + order.getAccount() + " money:" + order.getMoney());
						bossService.doPost(order);
						SysContext.ORDER_LIST.add(order);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
//						log.log(Priority.ERROR, "订单已经存在，请勿重复充值" + e.getMessage());
//						if(order != null)
//						{
//							order.setClbz(SysContants.CZ_UNKNOW);
//							order.setCljg("订单请勿重复提交");
//							new BusServiceImpl().doBusResult(order);
//						}
						try {
							Thread.sleep(3000);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							log.log(Priority.INFO, "没有获取到交易记录---3秒后重新获取");
						}
					}
//				}
			}else{
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					log.log(Priority.INFO, "没有获取到交易记录---3秒后重新获取");
				}
			}
		}
	}
}
